package com.chanjiedata.changbs.module.main;
/**
 * 文件名 : AllowLogFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/27 13:49
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.adapter.MyAllocaLogAdapter;
import com.chanjiedata.changbs.base.BaseFragment;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.Mystock;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by AndroidIntexh1 on 2018/6/4.
 */

public class AllowLogFragment extends BaseFragment {
    @BindView(R.id.fragment_recycleView)
    RecyclerView mRecycleView;
    Unbinder unbind;

    private int page = 1; // 当前页数
    private int position;  // 当前viewPage的位置
    private int pagesize = 10;  //


    public static AllowLogFragment NewInstance(int instanceType) {
        Bundle bundle = new Bundle();
        bundle.putInt("position", instanceType);
        AllowLogFragment messageTypeFragment = new AllowLogFragment();
        messageTypeFragment.setArguments(bundle);
        return messageTypeFragment;
    }


    /**
     * 获取布局文件
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recycleview;
    }


    /**
     * 初始化操作步骤
     */
    @Override
    protected void init() {
        unbind = ButterKnife.bind(this, rootView);
        position = getArguments().getInt("position");
    }

    /**
     * 初始化界面
     */
    @Override
    protected void initView() {
        initRecycle();
        loadData();
    }

    /**
     * 加载数据
     */
    public void loadData() {
        Mystock.allocationLogList.Builder builder = Mystock.allocationLogList.newBuilder();
        builder.setType(String.valueOf(position));
        NetworkManager.INSTANCE.post(Apis.allocationLogList, builder.build().toByteArray(), page, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                List<Mystock.allocationLogList.List> lists = Mystock.allocationLogList.parseFrom(bytes).getListList();
                mAdapter.addData(lists);
                int size = lists.size();
                if (size < 10) {
                    mAdapter.loadMoreEnd(true);  // 没有分页操作
                } else {
                    mAdapter.loadMoreComplete();  // 没有分页操作
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                if (!TextUtils.isEmpty(errorMessage)) showToast(errorMessage);

            }
        });

    }


    private List<Mystock.allocationLogList.List> datas = new ArrayList<>();
    private MyAllocaLogAdapter mAdapter;

    /**
     * 实例化线性底部RecyclerView
     * *
     */
    private void initRecycle() {
        mRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecycleView.setAdapter(mAdapter = new MyAllocaLogAdapter(mContext, datas));
        mAdapter.bindToRecyclerView(mRecycleView);
        mAdapter.setEmptyView(R.layout.recycle_empty);
        // 每个item的点击事件
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(mContext, AllocationDetailActivity.class);
            intent.putExtra("id", datas.get(position).getId() + "");
            startActivityForResult(intent,100);

        });
        mAdapter.setOnLoadMoreListener(() -> {
            page = mAdapter.getData().size() / pagesize + 1;
            loadData();
        }, mRecycleView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbind.unbind();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbind = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 200)
        {
            datas.clear();
            mAdapter.setNewData(datas);
            page = 1;
            loadData();
        }
    }
}
